<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class MemberComment extends Model{
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $auto = ['create_time'];

    /**
     * 获取用户
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field='*'){
        $list = $this
            ->with(['mch','member'])
            ->where($where)
            ->field($field)
            ->paginate($pageSize);
        return $list;
    }

    public function mch()
    {
        return $this->hasOne('Mch', 'id', 'mc_id')->bind([
            'name'
        ]);
    }

    public function member(){
        return $this->hasOne('Member', 'id', 'mid')->bind([
            'nickname'
        ]);
    }

    public function setCreateTimeAttr(){
        return time();
    }

    public function checkIt($mc_id,$mid){
        $i = $this
            ->where('mc_id','eq',$mc_id)
            ->where('mid','eq',$mid)
            ->count();
        return $i?true:false;
    }

    /**
     * @param $id
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByArticleId($id=0){
        if(!$id){
          return false;
        }

        $m = new Article();
        $info = $m->field('mcid')->find($id);
        if(!$info){
            return false;
        }
        $res = $this->field('id,mc_id,mid')->where('mc_id','eq',$info['mcid'])
            ->select();
        return $res;
    }

    /**
     * 获取是否回复
     * @param int $id
     * @param int $mid
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMemberReply($id=0,$mid=0){
        if(!$mid){
            return false;
        }

        $m = new Article();
        $info = $m->field('mcid')->find($id);
        if(!$info){
            return false;
        }
        $res = $this
            ->where('mc_id','eq',$info['mcid'])
            ->where('mid','eq',$mid)
            ->count('id');
        return $res?true:false;
    }
}